// pages/allGoods/allGoods.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentSelect: "",
    allTabs: [],
    currentGoodsList: [],
    allGoodsLists: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({//获取滑块的高宽
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          imageUrl: app.globalData.imageUrl,
        });
      }
    });
    if(options.id){
        that.setData({
          currentSelect: options.id
        })
    }
    getClassify(that)
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   // getAllGoodsByClassify(this)
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  switchTab: function (e) {
   /* if (e.target.id == 100) {
      this.setData({
        currentGoodsList: this.data.allGoodsLists
      })
    } else {*/
  
      if (!this.data.allTabs[e.target.dataset.index].picture) {
        getGoodsByClassify(this, e.target.id, e.target.dataset.index)
      } else {
        this.setData({
          currentGoodsList: this.data.allTabs[e.target.dataset.index].picture,
          currentSelect: e.target.id
        })
      }
  //  }
  }
})

function getClassify(that) {
      wx.request({
        url: app.globalData.allUrl + "/goods/clist",
        data: {
        },
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        // header: {}, // 设置请求的 header
        success: function (res) {
          if (that.data.currentSelect){
            getGoodsByClassify(that, that.data.currentSelect, 0)
          }else{
            getGoodsByClassify(that, res.data[0].type, 0)
          }
       //   console.log(res.data)
          that.setData({
            allTabs: res.data,
          })
        }
      })
}

function getGoodsByClassify(that, id, index) {
    wx.request({
      url: app.globalData.allUrl + "/goods/goods_class",
      data: {
        types: id,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
      //  console.log(res.data)
        //that.data.allTabs[index].picture = res.data;
        for (let i = 0; i < that.data.allTabs.length; i++) {
          if (that.data.allTabs[i].type == id) {
            that.data.allTabs[i].picture = res.data
          }
        }
        that.setData({
          allTabs: that.data.allTabs,
          currentGoodsList: res.data,
          currentSelect:id
        })
      }
    })
}
